<template>
  <div class="app-container">

  <div class="filter-container" style="margin-bottom:10px;">
      <el-input v-model="listQuery.searchword" placeholder="手机/账号" style="width: 200px;"  @clear="handleFilter" clearable class="filter-item" @keyup.enter.native="handleFilter" />
      
      <el-select v-model="listQuery.status" placeholder="状态"    @clear="clearStatus"
        @change="handleFilter" clearable style="width: 120px" class="filter-item">
        <el-option v-for="item in status" 
        :key="item.value"
        :label="item.label"
        :value="item.value"
       />
      </el-select>

      <el-select v-model="listQuery.role" placeholder="角色"   @clear="clearRole"
        @change="handleFilter" clearable style="width: 120px" class="filter-item">
        <el-option v-for="item in roleList" :key="item.value"
        :label="item.label"
        :value="item.value" />
      </el-select>
<!-- 
        <el-date-picker
      v-model="value1"
      type="datetime"
      placeholder="注册日期">
    </el-date-picker> -->
      
      <el-button  class="filter-item" type="primary" icon="el-icon-search" style="margin-left:10px;" @click="handleFilter">
        搜索
      </el-button>

      <el-button
      v-if="isShow"
        class="filter-item"
        type="primary"
        icon="el-icon-edit"
        style="margin-left:10px"
        @click="handleCreate"
      >新建</el-button>
      
    </div>

    <el-table v-loading="listLoading" :data="list" border style="width: 60%">

        <el-table-column width="160px" align="center" label="手机">
        <template slot-scope="scope">
          <span>{{ scope.row.phone }}</span>
        </template>
      </el-table-column>

        <el-table-column width="100px" align="center" label="账号">
        <template slot-scope="scope">
          <span>{{ scope.row.account }}</span>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" align="center" label="状态" width="120px">
        <template slot-scope="{row}">
          <el-tag :type="row.status | statusFilter">{{ getStatusText(row.status)}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column  align="center" label="角色" width="120px">
        <template slot-scope="scope">
          <span>{{ getRoleText(scope.row.role) }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="120px">
        <template slot-scope="scope">
       
            <el-button size="mini" type="danger" v-if="scope.row.status==2" @click="StopUser(scope.row,scope.row.f_id)" >
            停用
          </el-button>
          <el-button size="mini" type="success" v-if="scope.row.status==3" @click="StartUser(scope.row,scope.row.f_id)" >
            启用
          </el-button>
         
        </template>
      </el-table-column>

      
      <el-table-column style="width: 20%" align="center" label="注册时间">
        <template slot-scope="scope">
          <span>{{resolvingDate(scope.row.createTime)}}</span>
        </template>
      </el-table-column>

       <el-table-column style="width: 20%" align="center" label="上次登录时间">
        <template slot-scope="scope">
          <span>{{ resolvingDate(scope.row.last_login_date) }}</span>
        </template>
      </el-table-column>
    </el-table>


    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :model="temp"
        label-position="left"
        label-width="70px"
        style="width: 400px; margin-left:50px;"
      >
       

        <el-form-item label="账号" prop="account">
          <el-input v-model="temp.account" placeholder="必填" />
        </el-form-item>
         <el-form-item label="密码" prop="password">
          <el-input v-model="temp.password" placeholder="必填" />
        </el-form-item>
    
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">保存</el-button>
      </div>
    </el-dialog>


    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.size" @pagination="getList" />
  </div>
</template>

<script>
import { getList,StartUser,StopUser,createUser } from '@/api/user'
import { resolvingDate } from '@/utils/index'
import { mapGetters } from "vuex"
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'User',
  components: { Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
           pickerOptions: {
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        },
      dialogStatus: "",
      dialogFormVisible: false,
      textMap: {
        update: "编辑",
        create: "新建管理员"
      },
      isShow:false,
      temp: {
        f_id: "",
        password: "",
        phone: "",
        status: "",
        update_date_limit: "",
        role: "",
        account: "",
      
      },
      list: null,
      total: 0,
      listLoading: true,
      status: [
      
        {
          value: 2,
          label: "正常"
        },
        {
          value: 3,
          label: "停用"
        }
      ],
      statusValue: "",
      roleList: [
      
        {
          value: 98,
          label: "管理员"
        },
        {
          value: 7,
          label: "用户"
        }
        
      ],
      roleValue: "",
      listQuery: {
        page: 1,
        size: 10,
        status:'',
        role:'',
        searchword:'',

      }
    }
  },
  created() {

    console.log("权限",this.roles);
    if(this.roles==99){
       this.isShow=true
    }
    this.getList()
  },
  computed: {
    ...mapGetters([ "f_id","roles"])
  },
  methods: {


 //重置数据
 resetTemp() {
      // this. recommenValue=''
      // this.typeValue=''
      this.temp = {
        user_id: "",
        password: "",
        phone: "",
        status: "",
        shopname: "",
        price: "",
        update_date_limit: "",
        role: "",
        username: "",
        account: "",
        city: "",
        province: "",
        address: "",
        zid: "",
        bossid: "",
        createid: "",
        childnum: "",
        uuid1: "",
        uuid2: "",
        uuid3: ""
      };
    },
    // 新建用户
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = "create"
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate()
      })
    },
    createData() {
      createUser(this.temp).then(response => {
        this.temp = response.data
        this.list.unshift(this.temp)
        this.dialogFormVisible = false
        this.$notify({
          title: "Success",
          message: "创建成功",
          type: "success",
          duration: 2000
        })
      })
    },
    handleFilter() {
      this.getList()
    },


    getList() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.totalElements
        this.listLoading = false
      })
    },
    resolvingDate,
     // 获得role的文字
     getRoleText(role) {
      var roleText = "超级管理员"
      for (var index in this.roleList) {
        if (this.roleList[index].value === role) {
          roleText = this.roleList[index].label
        }
      }
      return roleText
    },
    // 获得status 的文字
    getStatusText(statu) {
      var statuText = "未知"
      for (var index in this.status) {
        if (this.status[index].value === statu) {
          statuText = this.status[index].label
        }
      }
      return statuText
    },
    // 获得status 的文字
    getStatusBtn(statu) {
      var statuText = "停用"
      if (statu === 3) {
        statuText = "启用"
      }
      return statuText
    },

    clearRole() {
      this.listQuery.role = ''
    },
    clearStatus() {
      this.listQuery.status = ''
    },

  StartUser(row,f_id) {
    var operate_id=this.f_id
            StartUser(f_id,operate_id).then(response => {
              this.temp = response.data
              for (const v of this.list) {
                if (v.f_id === this.temp.f_id) {
                  const index = this.list.indexOf(v)
                  this.list.splice(index, 1, this.temp)
                  break
                }
              }

              this.$notify({
                title: "Success",
                message: "启用成功",
                type: "success",
                duration: 2000
              })
            })
},


StopUser(row,f_id) {
    var operate_id=this.f_id
    StopUser(f_id,operate_id).then(response => {
              this.temp = response.data
              for (const v of this.list) {
                if (v.f_id === this.temp.f_id) {
                  const index = this.list.indexOf(v)
                  this.list.splice(index, 1, this.temp)
                  break
                }
              }

              this.$notify({
                title: "Success",
                message: "停用成功",
                type: "success",
                duration: 2000
              })
            })
},

  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
